<template>
	<!-- 博客页面 -->
	<view class="bod">

		<!-- 状态选择 -->
		<view class="tabss-border">
			<view class="tabss">
				<view :class="{active : isOne}" @click="changeId(0)">
					默认
				</view>
				<view :class="{active : isTwo}" @click="changeId(1)">
					最新
				</view>
				<view :class="{active : isThree}" @click="changeId(2)">
					热门
				</view>
			</view>
		</view>

		<!-- 搜索框 -->
		<view class="search">
			<view class="input">
				<u-input maxlength="20" placeholder="请输入搜索内容" trim border-color="#2979ff" v-model="keyword" type="text"
					:border="true" />
			</view>
			<view class="icon">
				<image src="../../static/search.png" mode="" @click="search"></image>
			</view>
		</view>

		<!-- 博客列表 -->
		<view class="blogList">
			<view v-for="(item,index) in blogData" :key="item.blog_id" class="blogList-item"
				@click="toBlogDetail(item.blog_id)">
				<view class="blogList-item-title">
					<text class="u-line-1">{{item.title}}</text>
				</view>
				<view class="blogList-item-content">
					<view class="blogList-item-content-text ">
						<text class="u-line-2">{{item.intro}}</text>
					</view>
					<view class="blogList-item-content-img">
						<!-- <u-image width="100%" height="140rpx" :src="item.img" :fade="true" duration="1050"> -->
						<u-image width="100%" height="130rpx" :src="item.img" :fade="true" duration="500">
							<u-loading slot="loading"></u-loading>
						</u-image>
						<!-- <u-lazy-load :image="item.img">
						</u-lazy-load> -->
					</view>
				</view>
				<view class="blogList-item-bottom">
					<view class="one">
						<text style="padding-right: 20rpx;">浏览数:{{item.browse}}</text>
						<text style="padding-right: 20rpx;">点赞数:{{item.remarkSum}}</text>
						<text>评论数:{{item.praiseSum}}</text>
					</view>
					<view class="two">
						<text>{{item.createdAt}}</text>
					</view>
				</view>
				<u-divider half-width="100%" :use-slot="false" margin-top="20"></u-divider>
			</view>
		</view>

		<u-toast ref="uToast" />

		<!-- 底部触发加载更多 -->
		<u-loadmore :load-text="loadText" margin-bottom="30" v-show="!show" :status="status" />

		<Tabbar></Tabbar>
	</view>
</template>

<script>
	import throttle from "lodash/throttle";
	import debounce from "lodash/debounce";
	import {
		getBlogData,
		searchBlogData
	} from '../../utils/blog.js'

	export default {
		data() {
			return {
				scrollTop: 0,
				iconStyle: {
					fontSize: '32rpx',
					color: '#2979ff'
				},
				//请求博客数据的参数
				id: '0,DESC',
				pageNum: 1,
				pageSize: 7,
				//博客列表的数据
				blogData: [],
				//load的状态
				status: 'loadmore',
				//加载动画的显示与隐藏
				show: false,
				//改变加载更多的文字
				loadText: {
					loadmore: '点击加载更多内容',
					loading: '正在加载,请喝杯奶茶',
					nomore: '没有更多了'
				},
				//搜索内容关键字
				keyword: '',
				searchPageNum: 1,
				searchPageSize: 8
			};
		},
		computed: {
			//判断三种状态的计算
			isOne() {
				return this.id.indexOf('0') !== -1
			},
			isTwo() {
				return this.id.indexOf('1') !== -1
			},
			isThree() {
				return this.id.indexOf('2') !== -1
			}
		},
		watch: {
			async keyword(newVal) {
				if (newVal === '') {
					this.status = 'loadmore'
					this.pageNum = 1
					let result = await getBlogData(this.id, this.pageNum, this.pageSize)
					if (result.code === 200) {
						this.blogData = result.data
					}
				}
			}
		},
		methods: {
			//获取伯博客数据
			async getData(isTrue) {
				this.searchPageNum = 1
				let result = await getBlogData(this.id, this.pageNum, this.pageSize)
				if (result.code === 200) {
					if (isTrue) {
						this.blogData = [...result.data, ...this.blogData]
						setTimeout(() => {
							uni.stopPullDownRefresh()
						}, 1000)
						if (result.data.length === 0) {
							this.$refs.uToast.show({
								title: '没有更多内容了!',
								type: 'warning',
								duration: 1000
							})
						}
					} else {
						this.blogData = [...this.blogData, ...result.data]
						this.status = 'loadmore'
						if (result.data.length === 0) {
							this.status = 'nomore'
						}
					}
				}
			},
			//跳转去博客详情
			toBlogDetail(id) {
				uni.navigateTo({
					url: `/subpkg/blogDetail/blogDetail?blog_id=${id}`
				})
			},
			//改变选择状态
			changeId: throttle(async function(num) {
				uni.pageScrollTo({
					scrollTop: 0,
					duration:0,
				});
				let Num = this.id.split(',')[0]
				let Type = this.id.split(',')[1]
				if (num === 0) {
					if (num == Num) {
						return
					} else {
						this.status = 'loadmore'
						this.pageNum = 1
						this.id = '0,DESC'
					}
				} else if (num === 1) {
					if (num == Num) {
						return
					} else {
						this.status = 'loadmore'
						this.pageNum = 1
						this.id = '1,DESC'
					}
				} else {
					if (num == Num) {
						return
					} else {
						this.status = 'loadmore'
						this.pageNum = 1
						this.id = '2,DESC'
					}
				}
				this.show = true
				this.searchPageNum = 1
				let result = await getBlogData(this.id, this.pageNum, this.pageSize)
				if (result.code === 200) {
					this.blogData = result.data
					this.show = false
				}
				this.keyword = ''
			}, 200),
			//搜索
			async search(isTrue) {
				if (this.keyword === '') {
					return
				}
				if (this.searchPageNum === 1) {
					this.blogData = []
				}
				this.pageNum = 1
				let result = await searchBlogData(this.keyword, this.searchPageNum, this.searchPageSize)
				if (result.code === 200) {
					if (isTrue) {
						this.blogData = [...result.data, ...this.blogData]
						setTimeout(() => {
							uni.stopPullDownRefresh()
						}, 1000)
						if (result.data.length === 0) {
							this.$refs.uToast.show({
								title: '没有更多内容了!',
								type: 'warning',
								duration: 1000
							})
						}
					} else {
						this.blogData = [...this.blogData, ...result.data]
						this.status = 'loadmore'
						if (result.data.length === 0) {
							this.status = 'nomore'
						}
					}
				}
			}
		},
		//下拉拉到底的时候触发的方法
		onReachBottom() {
			if (this.status === 'nomore') {
				return
			} else {
				this.status = 'loading'
				setTimeout(() => {
					if (this.keyword === '') {
						this.pageNum++
						this.getData()
					} else {
						this.searchPageNum++
						this.search()
					}
				}, 1000)
			}
		},
		//上拉刷新
		onPullDownRefresh() {
			setTimeout(() => {
				if (this.keyword === '') {
					this.pageNum++
					this.getData(true)
				} else {
					this.searchPageNum++
					this.search(true)
				}
			}, 500)
		},
		onLoad() {
			this.getData()
		}
	}
</script>

<style lang="scss" scoped>
	.bod {

		.tabss-border {
			// height: 44px;
			padding-top: 44px;
			width: 100%;
			background-color: white;
			position: sticky;
			top: 0;
			z-index: 999;

			.tabss {
				padding-top: 20rpx;
				display: flex;
				justify-content: space-between;
				width: 50%;
				margin: 0 auto;


				view {
					display: flex;
					justify-content: center;
					width: 33.33%;
					margin: 0 20rpx;
					font-size: 30rpx;
					font-weight: bold;
					height: 60rpx;

					&.active {
						color: #2979ff;
						border-bottom: 3px solid #2979ff;
					}
				}
			}
		}

		.search {
			width: 90%;
			margin: 0 auto;
			padding: 20rpx 0;
			display: flex;
			align-items: center;

			.input {
				width: 90%;
			}

			.icon {
				width: 10%;
				padding-left: 25rpx;

				image {
					width: 100%;
					height: 45rpx;

				}
			}
		}

		.blogList {
			width: 90%;
			margin: auto;
			// display: flex;

			.blogList-item {
				width: 100%;
				margin-bottom: 25rpx;


				.blogList-item-title {
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding-bottom: 25rpx;

					text {
						color: #303133;
						width: 100%;
						font-size: 35rpx;
						font-weight: bold;
					}

				}

				.blogList-item-content {
					width: 100%;
					display: flex;
					padding-bottom: 20rpx;


					.blogList-item-content-text {
						padding-top: 20rpx;
						flex: 7.5;
						color: #303133;
					}

					.blogList-item-content-img {
						flex: 2.5;
						padding-left: 20rpx;

						// image {
						// 	width: 100%;
						// 	height: 120rpx;
						// }
					}
				}

				.blogList-item-bottom {
					display: flex;
					justify-content: space-between;

					.one {
						font-size: 26rpx;
						color: #909399;
						display: flex;
						justify-content: space-between;
					}

					.two {
						font-size: 25rpx;

						color: #909399;
					}
				}
			}
		}

		.lazy {
			width: 30%;
			margin: 0 auto;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}


	// .two{
	// 	width: 37%;
	// 	font-size: 12rpx;
	// 	color:#909399 ;
	// }
</style>
